home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9098 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: news.tek.com!mdhost!gaulandm
  2. From: gaulandm@mdhost.cse.tek.com (Michael Alan Gauland)
  3. Newsgroups: comp.lang.c++
  4. Subject: Can I overload "*" without class overhead?
  5. Date: 28 Feb 1996 17:13:55 GMT
  6. Organization: Tektronix, Inc, Beaverton, OR, USA
  7. Sender: gaulandm@mdhost (Michael Alan Gauland)
  8. Distribution: world
  9. Message-ID: <4h22gj$pqa@tekadm1.cse.tek.com>
  10. NNTP-Posting-Host: hardtime.cse.tek.com
  11.  
  12. I need to implement some fixed-point math routines.  I've got a set of
  13. 16-bit data, with an implied decimal point in the middle of the number.
  14. To multiply two such values requires a right shift so the decimal point
  15. of the result is in the right place:
  16.  
  17.     result = ((int32)a * b) >> 8;
  18.  
  19. This looks sloppy, and every place in the code I put such an operation
  20. increases the chances of getting it wrong.  It seems a natural place
  21. to take advantage of C++'s operator overloading.  My attempts to date,
  22. however, have been slow (though elegant).   I expect this is due to the
  23. added overhead of a struct vs. an int.  Is there any way to work around
  24. this?  Of course, I could use a fuction like "FixedPointMultiply(a,b)",
  25. but I'll lose the added security of rigid typing.
  26.  
  27. Can anyone offer any helpful advice, or is this just something elegant C++ can't
  28. do as efficiently as sloppy C?
  29.  
  30. Thanks,
  31. Mike
  32.  
  33. --------------------------------------------------- 
  34. Michael A. Gauland    gaulandm@mdhost.cse.tek.com  
  35. AA7JF            (503) 627-5067
  36.  
  37.